<?php
class label_contentModel extends baseModel{
    protected $table = 'content';

    public function content_list($data){
        //解析
        $where=array();
        if(!empty($data['mid'])){
            $where['mid']=' AND B.mid in(' . $data['mid'] . ')';
        }
        if(!empty($data['cid'])){
            $where['cid']=' AND B.cid in(' . $data['cid'] . ')';
        }
        if ($data['type']=='sub'&&!empty($data['cid'])) {
            $where['cid'] = " AND B.cid in (" . api('default','category_getcat',$data['cid']).")";
        }
        if (!empty($data['image'])) {
            if($data['image']=='true')
            {
                $where['image'] = ' AND A.image<>"" ';
            }else{
                 $where['image'] = ' AND A.image is Null ';
            }
        }
        if (!empty($data['where'])) {
            $where['where'] = ' AND '.$data['where'];
        }
        if (!empty($data['limit'])) {
            $where['limit'] = $data['limit'];
        }else{
            $where['limit'] = 10;
        }
        if(!empty($data['order'])){
            $where['order']=$data['order'];
        }
        if (!empty($data['rand'])) {
            $where['order'] = 'rand()';
        }
        //基本SQL组合
        $condition='A.status=1'.$where['mid'].$where['cid'].$where['image'].$where['where'];

        //标签内容列表SQL接口
        $ext_sql='';
        $ext_field='';
        $ext_where='';
        $api_sql=ApiHook('hook_label_content_list_sql',$data);
        if(!empty($api_sql)){
            foreach ($api_sql as $value) {
                $ext_sql.=$value['sql'];
                $ext_field.=$value['field'];
                $ext_where.=$value['where'];
            }
        }

        //排序
        if($where['order']<>'rand()'){
            if(substr($where['order'],1)<>'B'||substr($where['order'],1)<>'C'){
                $where['order']='A.'.$where['order'];
            }
        }

        //获取相关文章
        if(!empty($data['related'])){
            if(preg_match("/[^\d-., ]/",$data['related'])){
                $tag_array=explode(',', $data['related']);
                $tagid='';
                if(!empty($tag_array)){
                    foreach ($tag_array as $value) {
                        $tag=$this->model->table('tags')->where('id='.$value)->find();
                        if(!empty($tag)){
                        $tagid.=$tag['id'].',';
                        }
                        unset($tag);
                    }
                }
            }else{
                $tag_array=$this->model->table('tags_relation')->where('aid in('.$data['related'].')')->find();
                if(!empty($tag_array)){
                    $tagid='';
                    foreach ($tag_array as $value) {
                        $tagid.=$value['tid'].',';
                    }
                }

            }
            $tagid=substr($tagid,0,-1);
            if(!empty($tagid)){
                $tags_relation=$this->model->table('tags_relation')->where('tid in('.$tagid.')')->find();
                if(!empty($tags_relation)){
                    $tags_aid='';
                    foreach ($tags_relation as $value) {
                        $tags_aid.=$value['aid'].',';
                    }
                    $tags_aid=substr($tags_aid, 0,-1);
                }
            }
            if(!empty($tags_aid)){
                $related=" AND A.aid in(".$tags_aid.") ";
                if(preg_match("/[^\d-., ]/",$data['related'])){
                    $related.=' AND A.aid<>'.$related_id;
                }
            }else{
                return ;
            }
        }


        if(!empty($data['content'])) {
            $content_field='C.content,';
            $content_sql=" LEFT JOIN {$this->model->pre}content_data C ON A.aid = C.aid ";
        }

        $sql="
            SELECT {$ext_field}{$content_field}A.*,B.name as cname,B.subname as csubname,B.mid
             FROM {$this->model->pre}content A 
             LEFT JOIN {$this->model->pre}category B ON A.cid = B.cid
             {$content_sql}
             {$ext_sql}
             WHERE {$condition}{$related}{$ext_where} ORDER BY {$where['order']} LIMIT {$where['limit']}
            ";
        $data=$this->query($sql);
        if(!empty($data)){
            $i=0;
            foreach ($data as $key=>$value) {
                $i++;
                $list[$key]=$value;
                $list[$key]['aurl']=$this->get_aurl($value);
                $list[$key]['i']=$i;
            }
        }
        return $list;
    }



    //内容超链接
    public function get_aurl($data,$page=false)
    {
        if(!is_array($data)){
            $data=$this->find('aid='.$data);
        }
        $sql="
            SELECT A.cid,A.urlname,B.model
            FROM {$this->model->pre}category A
            INNER JOIN {$this->model->pre}model B ON A.mid = B.mid
            WHERE A.cid={$data['cid']} ORDER BY A.cid desc LIMIT 1
            ";
        $info=$this->query($sql);
        $info=$info[0];

        if(empty($info)){
            return;
        }

        $config=config('APP');
        if($config['URL_REWRITE_ON']){
            $rewrite = config('REWRITE');
            $rewrite = array_flip($rewrite);
            $url=$rewrite[$info['model'].'/info/index'];
            $parameter=array('aid'=>$data['aid']);
            if(!empty($url)){
                $parameter=array();
                if(strpos($url,'<aid>')){
                    $parameter['aid']=$data['aid'];
                }
                if(strpos($url,'<urltitle>')){
                    $parameter['urltitle']=$data['urltitle'];
                }
                if(strpos($url,'<dir>')){
                    $parameter['dir']=$info['urlname'];
                }
                if(strpos($url,'<dirs>')){
                    $nav_list=api('default','category_nav',$data['cid']);
                    if(!empty($nav_list)){
                        $dirs='';
                        foreach ($nav_list as $value) {
                            $dirs.=$value['urlname'].'/';
                        }
                    }
                    $parameter['dirs']=trim($dirs,'/');
                }
                if(strpos($url,'<yy>')){
                    $parameter['yy']=date('Y',$data['inputtime']);
                }
                if(strpos($url,'<y>')){
                    $parameter['y']=date('y',$data['inputtime']);
                }
                if(strpos($url,'<m>')){
                    $parameter['m']=date('m',$data['inputtime']);
                }
                if(strpos($url,'<d>')){
                    $parameter['d']=date('d',$data['inputtime']);
                }
            }
        }else{
            $parameter=array('aid'=>$data['aid']);
        }
        if($page){
            $pagearray=array('page'=>'{page}');
            $parameter=array_merge($parameter,$pagearray);
        }
        return url($info['model'].'/info/index',$parameter);
    }


}
